﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebAPlTest
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HomeController : ControllerBase
    {
        private readonly IDataService dataService;
        public HomeController(IDataService dataService)
        {
            this.dataService = dataService;
        }
        public void ShowData()
        {
            //1.需要自己去实例化类 一旦类发生修改 就会影响很多地方
            //DataService dataService = new DataService();
            // dataService.GetData();

            //2.依赖注入 把new实例化 这一步交给ioc容器
            var data = dataService.GetAllBooks();
        }
        /// <summary>
        /// 构造注入
        /// </summary>
        /// <param name="dataService"></param>
        public HomeController(IDataService dataService)
        {
            _dataService = dataService;
        }
        //方法注入
        public void GetData([FromServices] IDataService dataService2)
        {
            dataService2.GetData();
        }
    }
}
